
def GetState(Line):
    Address3=Line[5]
    Address2=Line[4]
    if(Address3.strip()):
        Address=Address3.strip()
    elif(Address2.strip()):
        Address=Address2.strip()
    else:
        return 'SC'
    AddressList=Address.split(' ')
    return AddressList[-1]
        
def MakeSureYearExists(Year, ActiveIndicator):
    if(ActiveIndicator):
        Dictionary = ActiveDictionary
    else:
        Dictionary = InactiveDictionary
    if(Dictionary.has_key(Year)):
        return
    Dictionary[Year]={}

def MakeSureStateExists(Year, State, ActiveIndicator):
    if(ActiveIndicator):
        Dictionary = ActiveDictionary
    else:
        Dictionary = InactiveDictionary
    if(Dictionary[Year].has_key(State)):
        return
    Dictionary[Year][State]=[0.0, 0.0]
    



InputFileName='/home/robyg/Desktop/CapitalCredits_07_12_2010.csv'


reader = file(InputFileName)
headers=reader.next()

#File format is:  Mbrno, Name, Coaddr, addr1, addr2, addr3, Zip, Margins, Bad Debt, Net Margins, deadpatron, DiscDate, Mbrfee, Year_ims

#Format for splits:
#ActiveDictionary[Year][State][0] = Margin Total
#ActiveDictionary[Year][State][1] = Net Margin Total
#InActiveDictionary[Year][State][0] = Margin Total
#InActiveDictionary[Year][State][1] = Net Margin Total

ActiveDictionary={}
InactiveDictionary={}

count = 0

for textline in reader:
    line=textline.split(',')
    ActiveIndicator= (line[11].strip() == '000000')
    
    State=GetState(line).strip('\n')
    if(State=='ARKANSAS'):
        State='AK'
    elif(State in ['FLORIDA', 'FLA']):
        State='FL'
    elif(State=='VIRGINIA'):
        State='VA'   
    elif(State=='CALF'):
        State='CA'
    elif(State=='TEXAS'):
        State='TX'
    elif(State in ['SC-8838', '318', 'EASTOVER', 'COLUMBIA', 'HUNTLEY', 'ACCOUNT', 'PALMS', 'PALM', 'SWANSEA', '240912896', 'LEXINGTON', 'SCC', 'SCQ', 'LSC', 'HILL', 'C', 
                   '291120000', '291120000', '28570-6009', 'SANTEE', 'MATTHEWS', 'LCS', 'VANCE', 'EUTAWVILLE', 'SCSC', 'LCS', '240912896', 'HOPKINS', 'SANTEESC',
                   '29203-5886', '29113-9109', '29209-1747', 'CORNER', 'RD', 'J', ]):
        State='SC'
    elif(State in ['DELAWARE', 'DEL']):
        State='DE'
    elif(State=='ILL'):
        State='IL'
    elif(State=='OHIO'):
        State='OH'
    elif(State in ['MAINE', 'MAIN']):
        State='ME'
    elif(State in ['MASS',]):
        State='MA'
    elif(State in ['GEORGIA',]):
        State='GA'
    elif(State in ['WIS',]):
        State='WI'
    elif(State in ['DAKOTA',]):
        State='SD'
    elif(State in ['KENTUCKY',]):
        State='KY'
    elif(State in ['MISSOURI',]):
        State='MI'
    elif(State in ['TENN',]):
        State='TN'
    elif(State in ['MARYLAND',]):
        State='MD'
    elif(State in ['COLORADO',]):
        State='CO'
        
    Year=line[13].strip('\n')
    if(Year[0] in ['0', '1', '2']):
        Year = '20' + Year
    else:
        Year = '19' + Year
        
    Margins=line[7]
    NetMargins=line[9]
    MakeSureYearExists(Year, ActiveIndicator)
    MakeSureStateExists(Year, State, ActiveIndicator)
    count = count + 1
    if(ActiveIndicator):
        ActiveDictionary[Year][State][0] = ActiveDictionary[Year][State][0] + float(Margins)
        ActiveDictionary[Year][State][1] = ActiveDictionary[Year][State][1] + float(NetMargins)
    else:
        InactiveDictionary[Year][State][0] = InactiveDictionary[Year][State][0] + float(Margins)
        InactiveDictionary[Year][State][1] = InactiveDictionary[Year][State][1] + float(NetMargins)
                    




#Display Results

print 'Active Status, Year, State, Margins, NetMargins'
for year in ActiveDictionary:
    for state in ActiveDictionary[year]:
        print "Active, " + str(year) + ',' + str(state) + ',' + str(ActiveDictionary[year][state][0]) + ',' + str(ActiveDictionary[year][state][1])

for year in InactiveDictionary:
    for state in InactiveDictionary[year]:
        print "Inactive, " + str(year) + ',' + str(state) + ',' + str(InactiveDictionary[year][state][0]) + ',' + str(InactiveDictionary[year][state][1])
        
    
reader.close()

